<?php
/**
 * text
 *
 * script
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex_ez
 * @created:	27.01.2009 01:52:13
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */


class newsController extends Controller
{
	protected $quantity;
	
	public function onstart( )
	{
		$this ->visitor = visitor::__getMe( );
		$this ->model = new news( );
		$this ->quantity = _::pick( config::__getMe()->news_quantity, 10 );
		$this ->crumbs['/news/'] = 'Новости';
	}
	
	public function showIndexPage( $p, $o )
	{
		$page = (isset($p['page'])) ? (int)$p['page'] : 1;
		
		$list = $this ->model ->getBy( array( ), 'date-,tm-', array( ($page-1)*$this ->quantity, $this ->quantity) );
		$found_rows = $this ->model ->getFoundRows( );
		$total_pages = ( intval( $found_rows / $this ->quantity) ) + (( $found_rows % $this ->quantity)>0 ? 1:0 );
		
		$r = array( 'list' => $list, 'total_pages' => $total_pages, 'current_page' => $page, 'title' => 'Новости' );
		
		return $r;
	}

	public function showItemPage( $p, $o )
	{
		if( !isset( $p['id'] ) ) response::__getme( ) ->notfound( );
		
		$id = (int)$p['id'];
		$item = $this ->model ->get( $id );
		
		$r = array( );
		$r['item'] = $item;
		
		$items = $this ->model ->getAbove( $id );
		$r += $items;
		
		// meta & crumbs
		$meta = array( );
		$meta['title'] = $item['title'].' — '.'Новости';
		$meta['keywords'] = '';
		$meta['short'] = texo::run( $item['short'], "plain r|\n|\s" );
		$this ->meta = $meta;
		
		$this ->crumbs['/news/'.$id.'/'] =
			$r['title'] = $item['title'];
		
		return $r;
	}

	public function listBlock( $p )
	{
		$q = _::pick( @$p['quantity'], $this ->quantity );
		if( !$q ) $q = 3;
		
		$r = array( 'list' => $this ->model ->getBy( array( ), 'date-,tm-', $q ) );
		return $r;
	}
	
	public function showRSSPage( $p, $o )
	{
		$quantity = _::pick( @$p['quantity'], @$o['quantity'], $this ->quantity );
		$list = $this ->model ->getBy( array( ), 'date-,tm-', $quantity );
		//$v = view::__getMe( );
		//$v ->__Smarty ->assign( 'now', $list );
		//var_dump( $v );
		//die;
		response::__getMe( ) ->contentType = "application/rss+xml";
		response::__getMe( ) ->charset = "utf-8";
		return array( 'list' => $list, 'template' => '!rss.tpl', 'frame' => '' );
	}
}

?>